
var Q = require('q'),
    entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    CBOEnum = require('./Enum.server.controller');

// 创建一条设备监控记录
exports.insert = function(record) {
    var deferred = Q.defer();

    var entity = entityCollection.getEntity("DeviceMonitor");
    entity.insert(record, function(err, newRecord) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '新增设备监控错误';
            newErr.message = '请检查新增的设备监控记录参数！';
            deferred.reject(newErr);
            return;
        }
        deferred.resolve(newRecord);
    });

    return deferred.promise;
};

// 统计异常读头
exports.getExceptionDevices = function () {
    var deferred = Q.defer();

    var entity = entityCollection.getEntity("DeviceMonitor");
    entity.Entity
    .find({
        ExecuteResult: CBOEnum.ExecuteResult.AnalyzeException.Value,
        IsProcess: false
    })
    .distinct('TerminalUniqueID')
    .then(function (records) {
        deferred.resolve(records);
    }, function () {
        var newErr = new Error();
        newErr.leval = 9;
        newErr.title = '统计异常读头错误';
        newErr.message = '统计异常读头失败，请检查查询参数！';
        deferred.reject(newErr);
    });

    return deferred.promise;
};
